﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

using FarseerPhysics.Collision;
using FarseerPhysics.Common;


namespace Spacer {
	public class MainMenu : Activity {
		public void Initialize() {
			//initialize main menu texture and selection -CZ
			mainMenuSheet = Main.FindTexture("mainmenuwhite");
			gameLogo = Main.FindTexture("Project Spacer");
			selection = 0;
		}

		public void Shutdown() {
		}
		  
		public void Resumed() {
			if( Globals.CurrentSession != null ) {
				Globals.CurrentSession.Dispose();
				Globals.CurrentSession = null;
			}
			GC.Collect();
		}

		public void Paused() {
		}

		public void Update() {
			//calls the UpdateMenu function
			UpdateMenu();
		}

		public void DrawWorld() {
		}

		public void DrawOverlay() {
			//Draw Black Background, or BLACKGROUND!
			Surface.DrawColor = Color.Black;
			Surface.DrawRect(Vector2.Zero,new Vector2(Surface.Width,Surface.Height),Vector2.Zero,0f,Layer.LAYER_20);

			//Draw the game's logo
			Surface.DrawColor = Color.Yellow;
			Surface.DrawRect(gameLogo, new Vector2(Surface.Width/3-200, 50), new Vector2(Surface.Width - Surface.Width/3,150),new Vector2(0,0), 0f);

			//Selection notification
			//Surface.DrawColor = Color.Blue;
			//Surface.DrawRect(new Vector2(Surface.Width / 3, Surface.Height / 3 + selection * 70), new Vector2(500, 50), new Vector2(0, 0), 0f);

			//Draw the menu items
			for (int i = 0; i < 6; i++) {
				Surface.DrawColor = (selection == i ? Color.Aquamarine : Color.GreenYellow);
				if( i == 1 && !Helper.ExistsFile("Saves\\Galaxy\\Galaxy.xml") )
					Surface.DrawColor = Color.Gray;

				Surface.DrawSubRect(
					mainMenuSheet,
					new Vector2( Surface.Width / 3, Surface.Height / 3 + (i * 70) ),
					new Vector2( 500, 50 ),
					new Rectangle( (selection == i ? 500 : 0), i*70, 450, 70 ),
					new Vector2( 0, 0 ),
					0f
				);
			}
        } 

		public bool IsModal() {
			return false;
		}

		public bool IsTransparent() {
			return false;
		}

		public bool IsFinished() {
			return false;
		}

		/*Scrolls up and down through the  using W and S keys.
		Right now when enter is pressed, and New Game is selected, it brings you to the game. Otherwise it does nothing. */
		private void UpdateMenu(){

			//Scroll through selections
			if ((Input.IsKeyDown(Keys.Down) && !Input.WasKeyDown(Keys.Down)) ||
				(Input.IsGamePadButtonDown(GamePadButton.Down) && !Input.WasGamePadButtonDown(GamePadButton.Down)))
			{
				selection++;
				if (selection >= 6)
					selection = 0;
			}
			else if ((Input.IsKeyDown(Keys.Up) && !Input.WasKeyDown(Keys.Up)) ||
					 (Input.IsGamePadButtonDown(GamePadButton.Up) && !Input.WasGamePadButtonDown(GamePadButton.Up)))
			{
				selection--;
				if (selection < 0)
					selection = 5;
			}

			//If selection is 0 and enter is pressed, start the game
			if ((Input.IsKeyDown(Keys.Enter) && !Input.WasKeyDown(Keys.Enter)) ||
				(Input.IsGamePadButtonDown(GamePadButton.A) && !Input.WasGamePadButtonDown(GamePadButton.A)))
			{
				if (selection == 0) {
					Helper.DeleteDir( "Saves" );
					Helper.CreateDir( "Saves" );
					Helper.CreateDir( "Saves\\Galaxy" );
					Helper.CreateDir( "Saves\\Galaxy\\Players" );

					Globals.PushActivity( new PleaseWait(
						() => {
							return new Galaxy( typeof(GalaxyGenerator) );
						},
						galaxy => {
							Globals.CurrentSession = new GalaxySession( (Galaxy)galaxy );
							Globals.CurrentSession.IsHost = true;

							Globals.PushActivity( Globals.CurrentSession );
						}
					) );
                }
                else if (selection == 1)
                {
                    if( Helper.ExistsFile("Saves\\Galaxy\\Galaxy.xml") ) {
						Globals.PushActivity( new PleaseWait(
							() => {
								Galaxy galaxy = XmlObjectCfg<Galaxy>.Load( "Saves\\Galaxy\\Galaxy.xml" );
								galaxy.PostLoad();
								return galaxy;
							},
							galaxy => {
								Globals.CurrentSession = new GalaxySession( (Galaxy)galaxy );
								Globals.CurrentSession.IsHost = true;

								Globals.PushActivity( Globals.CurrentSession );
							}
						) );
					}
                }
                else if (selection == 2)
                {
                    // NETWORK LIST
                    Globals.PushActivity( new ConnectionsMenu() );
                }
                else if (selection == 3)
                {
                    Globals.PushActivity(new HowToPlay());
                }
                else if (selection == 4)
                {
                    //Globals.PushActivity(new ConnectionsMenu());
                    Globals.PushActivity(new CreditsScreen());
                }
                else if (selection == 5)
                {
                    Globals.GameInstance.Exit();
                }
			}
		}

		private Texture2D mainMenuSheet { get; set; }
		private Texture2D gameLogo { get; set; }
		private int selection { get; set; }
	}
}
